//ajax request variable
var AjaxReq = null;

//get xmlhttprequest instance
function getXHR() {
	if (window.XMLHttpRequest) {
		return new XMLHttpRequest();
	} else try {
		return new ActiveXObject('Msxml2.XMLHTTP');
	} catch (e) {
		try {
			return new ActiveXObject("Microsoft.XMLHTTP");
		} catch (e) {
			return null;
		}
	}
}

//update tab, true if succced and false otherwise
function updateContent(url, elementid) {
	AjaxReq = getXHR();
	if (AjaxReq!=null) {
		//create http get request
		AjaxReq.open('GET', url, true);
		
		//create callback
		AjaxReq.onreadystatechange = function() {
			
			//if response received
			if (AjaxReq.readyState==4) {
				//if success
				if (AjaxReq.status==200) {
					document.getElementById(elementid).innerHTML = AjaxReq.responseText;
				} else {
					document.getElementById(elementid).innerHTML = "Error. Could not retrieve data.";
				}
			} else {
				document.getElementById(elementid).innerHTML = "Loading content..";
			}
			return false;
		}
		
		//send
		AjaxReq.send('');
	} else {
		return false;
	}
	return true;
}

function getPictureComments(photoid) {
	var url = "http://localhost/deviantpic/service/comments.php?action=show_picture_comments&photo_id=" + photoid;
	updateContent(url, 'photo-comments');
}

function getWallComments(userid, wallid) {
	var url = "http://localhost/deviantpic/service/comments.php?action=show_wall_comments&user_id=" + userid + "&wall_id=" + wallid;
	var elementid = 'wall-comments-' + wallid;
	updateContent(url, elementid);
}

function getReplies(userid, messageid) {
	var url = "http://localhost/deviantpic/service/comments.php?action=show_message_replies&user_id=" + userid + "&message_id=" + messageid;
	var elementid = 'message-tail-' + messageid;
	updateContent(url, elementid);
}